package com.netintech.project.system.scheduler;

import com.netintech.common.exception.lossRateCheck.isCheckingException;
import com.netintech.project.system.ffmpeg.bo.FfmpegProperties;
import com.netintech.project.system.videoPacketLossRate.service.LossRateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时清理token表中失效数据
 *
 * @author luda
 */
@Slf4j
@Component
@EnableConfigurationProperties(FfmpegProperties.class)
public class CheckLossRateFromExcel {
    @Autowired
    private LossRateService lossRateService;
    @Autowired
    private FfmpegProperties ffmpegProperties;


    /**
     * 每天1点
     */
//    @Scheduled(cron = "0 0 1 * * *")
//    public void executeFromExcel() {
//        log.info("开启定时计划，读取excel，检测丢包率，将结果写入文件");
//        try {
//            lossRateService.executeFromExcel(ffmpegProperties.getCheckIntervalMilliSeconds(), ffmpegProperties.getTransportType());
//        } catch (LossRateException e) {
//            e.printStackTrace();
//            log.error(e.getMessage());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        log.info("检测丢包率定时计划已完成");
//    }

    /**
     * 每天21点
     */
    @Scheduled(cron = "0 0 21 * * *")
    public void executeFromDb() {
        log.info("开启定时计划，读取数据库，检测丢包率，将结果写入文件");
        Exception exception = null;
        try {
            lossRateService.executeFromDb(ffmpegProperties.getCheckIntervalMilliSeconds(), ffmpegProperties.getTransportType());
        } catch (Exception e) {
            exception = e;
            e.printStackTrace();
            log.error("定时计划检测丢包率失败", e);
        } finally {
            lossRateService.dbInit(exception);
            log.info("检测丢包率定时计划已完成");
        }
    }
}
